$(()=>{
    let addrId = $("#user-addr").val();
    let url = ctx + "/personal/addr";
    $.ajax({
        url,
        method:"get",
        dataType:"json",
        data:{
            id:addrId
        },success(resp){
            if (resp.success){
                //数据回显
                let addr = resp.data;
                $("#receiver").val(addr.receiver);
                $("#receiverPhone").val(addr.receiverPhone);
                $("#province").val(addr.province);
                $("#city").val(addr.city);
                $("#area").val(addr.area);
                $("#street").val(addr.street);
                $("#addrDetail").val(addr.addrDetail)
            }
        }
    });

    //地址改变事件
    $("#user-addr").change(function (){
        let addrId = $(this).val();
        let url = ctx + "/personal/addr";
        $.ajax({
            url,
            method:"get",
            dataType:"json",
            data:{
                id:addrId
            },success(resp){
                if (resp.success){
                    //数据回显
                    let addr = resp.data;
                    $("#receiver").val(addr.receiver);
                    $("#receiverPhone").val(addr.receiverPhone);
                    $("#province").val(addr.province);
                    $("#city").val(addr.city);
                    $("#area").val(addr.area);
                    $("#street").val(addr.street);
                    $("#addrDetail").val(addr.addrDetail)
                }
            }
        });
    });

    $("#addrAdd").click(function (){
        let addrId = $("#user-addr").val();
        console.log("111")
        layer.open({
            type: 2,
            title: '地址添加',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '500px'],
            content: ctx + '/personal/add1rAdd?id='+addrId
        });
    });
    $("#addrEdit").click(function (){
        console.log("111")
        let addrId = $("#user-addr").val();
        let receiver = $("#receiver").val();
        let receiverPhone = $("#receiverPhone").val();
        let province = $("#province").val();
        let city = $("#city").val();
        let area = $("#area").val();
        let street = $("#street").val();
        let addrDetail = $("#addrDetail").val();
        let url = ctx + "/personal/add1rEdit";
        $.ajax({
            url,
            method:"patch",
            dataType:"json",
            data:{
                id:addrId,
                receiver,
                receiverPhone,
                province,
                city,
                area,
                street,
                addrDetail
            },success(resp){
                if (resp.success){
                    layer.msg("修改成功!")
                    setTimeout(function (){
                        window.top.location.reload();
                    },1000)
                }else{
                    layer.msg("修改失败,请联系客服或稍后再试");
                }
            }
        })
    })
    $("#userEdit").click(function (){
        let username = $("#username").val();
        let oldPassword = $("#password").val();
        let password = $("#newPassword").val();
        let againPassword = $("#againPassword").val();
        let wechat = $("#wechat").val();
        let phone = $("#phone").val();
        let url = ctx + "/user/edit";
        if (password !== againPassword){
            layer.msg("两次密码不一致");
        }else if (username ===''){
            layer.msg("用户名不能为空");
        }else  if (oldPassword ===''){
            layer.msg("请输入原密码");
        }else if (password ===''){
            layer.msg("新密码不能为空");
        }else if (againPassword === ''){
            layer.msg("请再次确认密码");
        }else{
            $.ajax({
                url,
                method:"patch",
                dataType:"json",
                data:{
                    username,
                    password:oldPassword,
                    newPassword:password,
                    wechat,
                    phone
                },success(resp){
                    if (resp.error){
                        layer.msg(resp.error)
                    }else if (resp.success){
                        layer.msg("修改成功,请重新登录")
                        setTimeout(function (){
                            location.href = ctx +"/user/login";
                        },1000)
                    }
                }
            })
        }

    })


})